package com.hissy.cn.service;

import java.util.List;
import java.util.stream.Collectors;

import com.hissy.cn.dao.dto.UserDTO;
import com.hissy.cn.dao.dto.UserDTOExample;
import com.hissy.cn.dao.generatedMapper.UserDTOMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by zhangyanfei on 2019/1/22.
 *
 * @author zhangyanfei
 * @date 2019/01/22
 */
@Service
public class UserService {
    @Autowired
    private UserDTOMapper userDTOMapper;

    public List<UserInfo> getUserInfoList(){
        UserDTOExample userDTOExample=new UserDTOExample();
        userDTOExample.createCriteria().andIdGreaterThan(0);
        return userDTOMapper.selectByExample(userDTOExample).stream().map(this::convertToUserInfo).collect(
            Collectors.toList());
    }

    @Transactional(rollbackFor = Exception.class)
    public void addUser(){
        UserDTO userDTO=new UserDTO();
        userDTO.setAge(10);
        userDTO.setUserName("zhangsan123");
        userDTOMapper.insert(userDTO);
    }

    public UserInfo convertToUserInfo(UserDTO userDTO){
        UserInfo userInfo=new UserInfo();
        userInfo.id=userDTO.getId();
        userInfo.age=userDTO.getAge();
        userInfo.userName=userDTO.getUserName();
        return userInfo;
    }

}
